/**
 * 删除有序列表中重复的元素
 */

#include <stdio.h>
#include <stdlib.h>

#define bool int
#define true !0
#define false 0

typedef struct LNode{
	struct LNode* next;
	int data;
} LNode, *LinkedList;

bool init(LinkedList &list);

bool insert(LinkedList &list, int data);

void print(LinkedList &list);

bool core(LinkedList &A);

int main()
{
	LinkedList list=NULL;
	init(list);
	insert(list,1);
	insert(list,2);
	insert(list,2);
	insert(list,2);
	insert(list,3);
	insert(list,5);
	insert(list,5);
	insert(list,5);
	insert(list,6);
	insert(list,8);
	insert(list,8);
	insert(list,9);
	insert(list,12);	
	print(list);
	core(list);
	print(list);
	return 0;
}

bool init(LinkedList &list)
{
	LNode* newNode=(LNode* )malloc(sizeof(LNode));
	if(!newNode) return false;
	newNode->next=NULL;
	list=newNode;
	return true;
}

bool insert(LinkedList &list, int data)
{
	LinkedList node=list;
	while(node->next) node=node->next;
	LNode* newNode=(LNode* )malloc(sizeof(LNode));
	if(!newNode) return false;
	newNode->next=NULL;
	newNode->data=data;
	node->next=newNode;
	return true;
}

void print(LinkedList &list)
{
	LinkedList node=list->next;
	while(node)
	{
		printf("%d ",node->data);
		node=node->next;
	}
	printf("\n");
}

bool core(LinkedList &A)
{
	LNode* node=A->next;
	LNode* next_node=NULL;		//指向遍历结点node的后继结点
	if(!node) return false;
	
	while(node->next)			
	{
		next_node=node->next;
		if(next_node->data==node->data)	//当前遍历结点和它的下一个结点值相同，则说明出现了重复
		{
			node->next=next_node->next;
			free(next_node);			//释放重复的结点
			next_node=NULL;
		} else {
			node=node->next;
		}
	}
	return true;
}